﻿var spa = spa || {};

spa.dataAdapter = function (controllerName) {
    /// <summary>
    /// Creates a DataAdapter to talk to a Web API Controller
    /// </summary>
    /// <param name="controllerName" type="String">
    /// Required: Name of the Controller
    /// </param>
    /// <return>
    /// DataAdapter
    /// </return>
    this._controllerUrl = "/api/" + controllerName;
};

spa.dataAdapter.prototype = {
    LoadOne: function (id, callBack) {
        /// <summary>
        /// Loads a single item (GET)
        /// </summary>
        /// <param name="id" type="var">
        /// Required: id of the entity
        /// </param>
        /// <param name="callBack" type="function (data)">
        /// Required: callback with the data
        /// </param>
        $.ajax({
            type: "POST",
            contentType: "application/json",
            dataType: "text json",
            url: this._controllerUrl + "/" + id,
            type: "GET",
            success: callBack,
        });
    },
    PostItem: function (data, callBack) {
        /// <summary>
        /// Adds a single item (POST)
        /// </summary>
        /// <param name="data" type="JSON">
        /// Required: data to post
        /// </param>
        /// <param name="callBack" type="function (data)">
        /// Required: callback with the data
        /// </param>
        $.ajax({
            type: "POST",
            contentType: "application/json",
            dataType: "text json",
            url: this._controllerUrl,
            data: data,
            success: callBack,
        });
    },
    PutItem: function (id, data, callBack) {
        /// <summary>
        /// Updates a single item (PUT)
        /// </summary>
        /// <param name="id" type="var">
        /// Required: id of the entity
        /// </param>
        /// <param name="data" type="JSON">
        /// Required: data to put
        /// </param>
        /// <param name="callBack" type="function (data)">
        /// Required: callback with the data
        /// </param>
        $.ajax({
            type: "PUT",
            contentType: "application/json",
            dataType: "text json",
            url: this._controllerUrl + "/" + id,
            data: data,
            success: callBack
        });
    },
    DeleteItem: function (id, callBack) {
        /// <summary>
        /// Deletes a single item (DELETE)
        /// </summary>
        /// <param name="id" type="var">
        /// Required: id of the entity
        /// </param>
        /// <param name="callBack" type="function ()">
        /// Required: callback when done
        /// </param>
        $.ajax({
            type: "DELETE",
            contentType: "application/json",
            dataType: "text json",
            url: this._controllerUrl + "/" + id,
            statusCode: {
                200: callBack.call(this),
            }
        });
    },
    QueryAll: function (callBack, query) {
        /// <summary>
        /// Gets multiple items (GET)
        /// </summary>
        /// <param name="callBack" type="function (data)">
        /// Required: callback with entities
        /// </param>
        /// <param name="query" type="String">
        /// Optional: odata QueryString
        /// </param>
        var url = this._controllerUrl;
        if (query) {
            url = url + "?" + query;
        }
        $.ajax({
            type: "GET",
            contentType: "application/json",
            dataType: "text json",
            url: url,
            success: callBack,
        });
    }
};